package com.maixy.rabbitmqdemo.normal;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName Sender
 * @Description 生产者
 * @Date 2021/3/23 20:34
 * @Author maixy
 */
@Component
public class Sender {
    @Resource
    private AmqpTemplate amqpTemplate;

    //直连模式生产者
    public void send1(){
        String content = "Sender says:" + "'hello， I'm sender'";
        System.out.println(content);
        //将消息发送到队列上
        amqpTemplate.convertAndSend("hello1", content);
    }

    //work queues模式队列
    public void send2(int i){
        String context=i+"";
        System.out.println(context+"--send:");
        amqpTemplate.convertAndSend("hello2",context);
    }

    //fanout
    public void fanout(){
        String context="hi,fanout msg";
        System.out.println("Sender:"+context);
        //将消息发送到交换机上
        amqpTemplate.convertAndSend("fanoutExchange","",context);
    }

    /***********************************Topic*****************************************/
    public void topic1(){
        String context="hi,i am message 1";
        System.out.println("Sender:"+context);
        //参数分别为：交换机,routing_key,消息内容
        amqpTemplate.convertAndSend("topicExchange","topic.message",context);
    }

    public void topic2(){
        String context="hi,i am message 2";
        System.out.println("Sender:"+context);
        amqpTemplate.convertAndSend("topicExchange","topic.messages",context);
        amqpTemplate.convertAndSend("topicExchange",context);
    }

    public void topic3(){
        String context="hi,i am message 3";
        System.out.println("Sender:"+context);
        amqpTemplate.convertAndSend("topicExchange",context);
    }
}
